package com.thjk.bdf.preloan.web.controller;  
  
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.thjk.bdf.common.utils.RedisUtils;



public class BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RedisUtils redisUtils;
	
    public String getOpenId(HttpServletRequest request){  
    	String cacheKey = getCacheKeyFromRequest(request);
    	if (!StringUtils.isEmpty(cacheKey))
    	{
	    	return (String)redisUtils.get(cacheKey);
    	}else{
	    	return null;
    	}
    }
    
    public void setOpenId(String openId, HttpServletRequest request){  
    	String cacheKey = getCacheKeyFromRequest(request);
    	if (!StringUtils.isEmpty(cacheKey))
    	{
	    	redisUtils.set(cacheKey, openId, 5*60L);
    	}else{
    		logger.info("get cacheKey from request empty");
    	}
    }
    
    protected String getCacheKeyFromRequest(HttpServletRequest request)
    {
    	String cacheKey = null;
        Cookie[] cookies = request.getCookies();
        if(null != cookies){            
            for (Cookie cookie : cookies)
            {
                if ("wxOpenId".equals(cookie.getName()))
                {
                    cacheKey = cookie.getValue();
                }
            }
        }
        if(StringUtils.isEmpty(cacheKey)){
        	HttpSession session = request.getSession();
        	if(session != null){
        		Object keyObject = session.getAttribute("wxOpenId");
        		if(keyObject != null){
        			cacheKey = keyObject.toString();
        		}
        	}
        }
        return cacheKey;
    }
    
    
    protected void setCacheKeyToResponse(HttpServletResponse response){
    	String cacheKey = "CookieId" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		Cookie cookie = new Cookie("wxOpenId", cacheKey);
		response.addCookie(cookie);
    }
    
    /**
     * 类转map
     * @param obj
     * @return
     * @throws Exception
     */
	protected Map<String, Object> convertBeanToMap(Object obj) throws Exception
    {
		Map<String, Object> map = new TreeMap<String, Object>();
		if (obj == null) {
			return map;
		}
		Field[] declaredFields = obj.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			int mod = field.getModifiers();
			if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
				continue;
			}			
			field.setAccessible(true);
			Object value = field.get(obj);
			if (null == value) {
				continue;
			}
			map.put(field.getName(), field.get(obj));
		}
		return map;
    }
	
}